package com.example.win.android_banduyujia.lib.util;

import android.content.SharedPreferences;

import com.example.win.android_banduyujia.BanDuApp;
import com.example.win.android_banduyujia.entity.LoginInfEntity;

/**
 * Created by Win on 2015/8/13.
 */
public class SharedPreferencesUtil {

    public final static String LOCAL_SAVE_NAME = "bandu";
    public final static String LOCAL_SAVE_USER_ID = "bandu_tokeninfo";
    public final static String LOCAL_SAVE_SEARCH_INFO = "bandu_searchinfo";

    private static void saveStringValue(String name, String value) {
        SharedPreferences  settings = BanDuApp.getContext().getSharedPreferences(LOCAL_SAVE_NAME, 0);
        SharedPreferences.Editor  editor = settings.edit();
        editor.putString(name, value);
        editor.commit();
    }

    private static String getStringValue(String name) {
        SharedPreferences  settings = BanDuApp.getContext().getSharedPreferences(LOCAL_SAVE_NAME, 0);
        return settings.getString(name, "");
    }

    public static void saveLoginInfo(LoginInfEntity loginInfo) {
        String  userId = loginInfo.getID();
        saveStringValue(LOCAL_SAVE_USER_ID, userId);
        saveStringValue(userId, loginInfo.getAccessToken());
    }

    public static void clearAccessToken() {
        String userId = getStringValue(LOCAL_SAVE_USER_ID);
        saveStringValue(userId, "");
    }

    public static String getAccessToken() {
        String userId = getStringValue(LOCAL_SAVE_USER_ID);
        return getStringValue(userId);
    }
}
